package com.parse.utils;

import com.parse.dto.Product;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.helper.StringUtil;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * Created by fangjicai on 2020/5/11.
 */
public class JsoupUtils {

    public List<Product> getData(List<String> urls) throws Exception{
        if(urls==null || urls.size()<1){
            return null;
        }
        List<Product> productContainer=new CopyOnWriteArrayList();
       //为每一个页面分配一个线程池，去抓取数据，提高效率--》充分利用多核cpu的资源

        //递减锁 每页的线程获取一把锁
        CountDownLatch countDownLatch = new CountDownLatch(urls.size());


        urls.forEach(url->{
            System.out.println("抓取："+url+"数据！");
            ThreadPoolUtils.getThreadPool().execute(new Runnable() {
                @Override
                public void run()  {
                    try{
                        //获取分页对象
                        Connection pageCon = Jsoup.connect(url);
                        Document page = pageCon.get();
                        //获取每页中商品item列表
                        Elements elements = page.select("li.item");
                        elements.forEach(e->{
                         //获取商品详情页链接
                         String productUrl= e.child(0).attr("href");
                            System.out.println("productUrl:"+productUrl);
                         if(!StringUtil.isBlank(productUrl)){
                             try {
                                 //获取详情页页面对象
                                 Document detailPage = Jsoup.connect(productUrl).timeout(10000).get();
                                 //获取真个商品document对象
                                 Elements product = detailPage.select("div.product-shop");
                                 //获取品牌字段
                                 String brand= product.select("div.product-designer").select("a").text();
                                 //获取商品名称
                                 String prodName = product.select("div.product-name").select("span").text();
                                 //获取商品价格
                                 String price = product.select("div.price-info").select("div.price-box").select("span.price").text();
                                 productContainer.add(new Product(brand, prodName, price));
                             }catch (Exception e2){
                                 System.out.println("e2:"+e2.getMessage());
                             }
                         }
                        });
                    }catch (Exception e){
                        System.out.println("e1"+e.getMessage());
                    }
                    //线程进行锁递减
                    countDownLatch.countDown();
                }
            });
        });
        //等待所有线程将数据抓取完毕,在执行返回操作
        countDownLatch.await();
        System.out.println("商品数量："+productContainer.size());
        return productContainer;
    }
}
